package com.example.userclient.adapter

import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.userclient.fragment.MineFragment
import com.example.userclient.fragment.ShopFragment

class MyFragmentAdapter(fragment:Fragment,private  val targetFragments: SparseArray<Fragment>) : FragmentStateAdapter(fragment){
    override fun getItemCount() :Int{
        return targetFragments.size()
    }

    override fun createFragment(position: Int): Fragment {
        // Return a NEW fragment instance in createFragment(int)
        val fragment:Fragment = when(position){

            0->
                ShopFragment.newInstance()

            1->
                MineFragment.newInstance()
            else->{
                throw Exception("no handler")
            }
        }
        targetFragments.put(position,fragment)
        return fragment
    }
}